//数据库相关操作
const mysql = require('mysql2/promise')   //导入模块
const config = require('../config/index.js')
//创建数据库连接
//创建线程池 (连接池)
const pool = mysql.createPool({
host:config.db.host,
port:config.db.port,
user:config.db.user,
password:config.db.password,
database:config.db.database
})

//执行sql的方法
const allServices={
  async query(sql,values){
        try{
            //通过连接池连接mysql
        const conn =await pool.getConnection()
        //执行各种增删改查的sql语句
       const [rows,fields] = await conn.query(sql,values)
       //释放连接
       pool.releaseConnection(conn)
       //返回结果
       return Promise.resolve(rows)
        }
        catch(error){
            //如果有错误,返回错误信息
            return Promise.reject(error)
        }
    }
}

//登录接口要执行的函数
const userLogin=(username,password)=>{
let _sql=`select * from user where username='${username}' and password='${password}';`
return allServices.query(_sql)
}
//查找账号是否存在
const findUser=(username)=>{
    let _sql=`select * from user where username='${username}';`
    return allServices.query(_sql)
}
//注册接口要执行的函数
const userRegister=(username,password,nickname,create_time)=>{
    let _sql=`insert into user (username,password,nickname,create_time) values ('${username}','${password}','${nickname}','${create_time}');`
    return allServices.query(_sql)
}
module.exports={
    userLogin,
    userRegister,
    findUser
}
